package com.android.hidephotosplus.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.android.hidephotosplus.R;
import com.android.hidephotosplus.model.ModelMedia;
import com.android.hidephotosplus.util.Config;
import com.android.hidephotosplus.util.MLoadMedia;
import com.android.hidephotosplus.util.RetainCache;

public class MediaAdapter extends ArrayAdapter<ModelMedia> {

	private Context context;

	private ArrayList<ModelMedia> arrMedias;

	private ViewHolder viewHolder;

	private MLoadMedia loadMedia = null;

	public boolean scroolState = false;

	private int widthAlbum;

	private boolean isPublic;

	public boolean isScroolState() {
		return scroolState;
	}

	public void setScroolState(boolean scroolState) {
		this.scroolState = scroolState;
	}

	@SuppressWarnings("deprecation")
	public MediaAdapter(Context context, ArrayList<ModelMedia> objects,
			boolean isPublic) {
		super(context, R.layout.item_media, objects);
		this.context = context;
		this.arrMedias = objects;
		this.isPublic = isPublic;

		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		widthAlbum = display.getWidth()
				/ context.getResources().getInteger(
						R.integer.number_columns_album_view) - 4;

		RetainCache c = RetainCache.getOrCreateRetainableCache();
		loadMedia = c.loadMedia;
		if (loadMedia == null) {
			loadMedia = new MLoadMedia(Config.onSize(context),
					Config.MAX_PIXELS_WIDTH, Config.MAX_PIXELS_HEIGHT);
			c.loadMedia = loadMedia;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.item_media, parent, false);
			viewHolder = new ViewHolder();
			viewHolder.imagePlay = (ImageView) view
					.findViewById(R.id.imagePlay);
			viewHolder.image = (ImageView) view.findViewById(R.id.image);
			viewHolder.imageCheck = (ImageView) view
					.findViewById(R.id.imageCheck);
			view.setTag(viewHolder);
		} else
			viewHolder = (ViewHolder) view.getTag();

		LayoutParams params = view.getLayoutParams();
		if (params != null) {
			params.height = widthAlbum;
			params.width = widthAlbum;
			view.setLayoutParams(params);
		}
		LayoutParams paramsPlay = viewHolder.imagePlay.getLayoutParams();
		if (paramsPlay != null) {
			paramsPlay.height = widthAlbum / 2;
			paramsPlay.width = widthAlbum / 2;
			viewHolder.imagePlay.setLayoutParams(paramsPlay);
		}

		if (position != -1 && position < arrMedias.size()) {
			ModelMedia item = getItem(position);
			if (item != null) {
				if (isPublic)
					loadMedia.loadBitmap(context, item.isVideo,
							item.getPathMediaStock(), viewHolder.image,
							isScroolState(), mNotifyRunnable, isPublic);
				else
					loadMedia.loadBitmap(context, item.isVideo,
							item.getPathMediaHidden(), viewHolder.image,
							isScroolState(), mNotifyRunnable, isPublic);

				if (item.isVideo) {
					viewHolder.imagePlay.setVisibility(View.VISIBLE);
				} else
					viewHolder.imagePlay.setVisibility(View.GONE);

				if (item.isChecked)
					viewHolder.imageCheck.setVisibility(View.VISIBLE);
				else
					viewHolder.imageCheck.setVisibility(View.GONE);

			}

		}

		return view;
	}

	private class ViewHolder {
		ImageView image;
		ImageView imagePlay;
		ImageView imageCheck;
	}

	Runnable mNotifyRunnable = new Runnable() {
		@Override
		public void run() {
			notifyDataSetChanged();
		}
	};

}
